import practice
import json_practice
import json
import datetime
import wx
import os
from http_rest_client import ReachOutHttpClient
from CreateCharityDialog import CreateCharityDialog
from DonateCharityDialog import DonateCharityDialog
from CreateTopicDialog import CreateTopicDialog
from Charity import Charity
from Identity import Identity
from Topic import Topic
# import the REST library

#charity = client.getCharityByName("PlanetGranite?")

#client.createCharity(json_practice.c)
#client.createTopic(json_practice.t)
#client.createComment(json_practice.c)
#client.getTopicByName("Climbing")
#client.getTopicDetails("Climbing")
#client.subscribe(json_practice.s)
class MainWindow(wx.Frame):
    def __init__(self, parent, title):
        self.client = ReachOutHttpClient()
        self.string = "hello"
        self.dirname = ''
        
        self.identity = Identity()
        self.topic = Topic()
        self.topic.setIdentity(self.identity)
        self.charity = Charity()
        self.charity.setIdentity(self.identity)
        # A "-1" in the size parameter instructs wxWidgets to use the default size.
        # In this case, we select 200px width and the default height.
        wx.Frame.__init__(self, parent, title=title, size=(200,-1))
        
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        text_font = wx.Font(15, wx.SWISS, wx.NORMAL, wx.NORMAL, False, u'Arial')
        # Setting up the menu.
#        filemenu= wx.Menu()
#        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
#        menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
#        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")

        # Creating the menubar.
#        menuBar = wx.MenuBar()
#        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
#        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        # Events.
#        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
#        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
#        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        
        """ Connection Setup """
        self.connectionPanel = wx.Panel(self)
        self.connectionSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.hostText = wx.StaticText(self.connectionPanel, label = "Host: ", style = wx.ALIGN_LEFT)
        self.portText = wx.StaticText(self.connectionPanel, label = "Port: ", style = wx.ALIGN_LEFT)
        self.hostText.SetFont(text_font)
        self.portText.SetFont(text_font)
        self.hostField = wx.TextCtrl(self.connectionPanel)
        self.portField = wx.TextCtrl(self.connectionPanel)
        self.button = wx.Button(self.connectionPanel, -1, "Set Connection")
        
        self.connectionSizer.Add(self.hostText)
        self.connectionSizer.Add(self.hostField)
        self.connectionSizer.AddSpacer(20)
        self.connectionSizer.Add(self.portText)
        self.connectionSizer.Add(self.portField)
        self.connectionSizer.AddSpacer(20)
        self.connectionSizer.Add(self.button)
        self.connectionPanel.SetSizer(self.connectionSizer)
        
        self.button.Bind(wx.EVT_BUTTON, self.setRootURL)
        
        """ Menu Setup """
        self.menuPanel = wx.Panel(self)
        self.displayText = wx.TextCtrl(self, style = wx.TE_MULTILINE | wx.TE_READONLY)
        
        self.controlSizer = wx.BoxSizer(wx.HORIZONTAL)
        
        self.controlSizer.Add(self.menuPanel, 0, wx.EXPAND)
        self.controlSizer.Add(self.displayText, 1, wx.EXPAND)
        
        
        self.menuSizer = wx.BoxSizer(wx.VERTICAL)
    
        self.charityTitle = wx.StaticText(self.menuPanel, label = "Charity", style = wx.ALIGN_LEFT)
        self.charityTitle.SetFont(text_font)
        self.discussionTitle = wx.StaticText(self.menuPanel, label = "Discussion", style = wx.ALIGN_LEFT)
        self.discussionTitle.SetFont(text_font)
        self.createCharityB = wx.Button(self.menuPanel, -1, "Create Charity", size=wx.Size(125,25))
        self.listCharitiesB = wx.Button(self.menuPanel, -1, "List Charities", size=wx.Size(125,25))
        self.getCharityByNameB = wx.Button(self.menuPanel, -1, "Charity Details", size=wx.Size(125,25))
        self.donateCharityB = wx.Button(self.menuPanel, -1, "Donate to Charity", size=wx.Size(125,25))
        
        self.createTopicB = wx.Button(self.menuPanel, -1, "Create Topic", size=wx.Size(125,25))
        self.listTopicsB = wx.Button(self.menuPanel, -1, "List Topics", size=wx.Size(125,25))
        self.createCommentB = wx.Button(self.menuPanel, -1, "Create Comment", size=wx.Size(125,25))
        self.getTopicByNameB = wx.Button(self.menuPanel, -1, "Get Topic By Name", size=wx.Size(125,25))
        self.getTopicDetailsB = wx.Button(self.menuPanel, -1, "Get Topic Details", size=wx.Size(125,25))
        self.line = wx.StaticLine(self.menuPanel)
        self.line2 = wx.StaticLine(self.menuPanel)
        
        self.menuSizer.Add(self.charityTitle)
        self.menuSizer.Add(self.line, flag = wx.EXPAND)
        self.menuSizer.Add(self.createCharityB)
        self.menuSizer.Add(self.listCharitiesB)
        self.menuSizer.Add(self.getCharityByNameB)
        self.menuSizer.Add(self.donateCharityB)
        self.menuSizer.AddSpacer(20)

        self.menuSizer.Add(self.discussionTitle)
        self.menuSizer.Add(self.line2, flag = wx.EXPAND)
        self.menuSizer.Add(self.createTopicB)
        self.menuSizer.Add(self.listTopicsB)
        self.menuSizer.Add(self.createCommentB)
        self.menuSizer.Add(self.getTopicByNameB)
        self.menuSizer.Add(self.getTopicDetailsB)
        
        self.menuPanel.SetSizer(self.menuSizer)

        # Use some sizers to see layout options
        self.connLine = wx.StaticLine(self)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        mainSizer.Add(self.connectionPanel, 0, wx.EXPAND)
        mainSizer.Add(self.connLine, 0, wx.EXPAND)
        mainSizer.Add(self.controlSizer, 1, wx.EXPAND)
 

        """ Button Event Bindings """
        
        self.createCharityB.Bind(wx.EVT_BUTTON, self.createCharity)
        self.donateCharityB.Bind(wx.EVT_BUTTON, self.donateCharity)
        self.getCharityByNameB.Bind(wx.EVT_BUTTON, self.getCharityByName)
        self.listCharitiesB.Bind(wx.EVT_BUTTON, self.listCharities)
        self.listTopicsB.Bind(wx.EVT_BUTTON, self.listTopics)
        self.createTopicB.Bind(wx.EVT_BUTTON, self.createTopic)
        self.getTopicByNameB.Bind(wx.EVT_BUTTON, self.getTopicByName)
        self.getTopicDetailsB.Bind(wx.EVT_BUTTON, self.getTopicDetails)

        #Layout sizers
        self.SetSizer(mainSizer)
        self.SetAutoLayout(1)
        mainSizer.Fit(self)
        self.Centre()
        self.Show()
        
    def setRootURL(self,e):
        rootURL = "http://" + self.hostField.GetValue() + ":" + self.portField.GetValue()  + "/ReachOut/rest/community"
        self.client.setRootURL(rootURL)

    def createCharity(self,e):
        """ Open a file"""
        dlg = CreateCharityDialog(self)
#        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(self.charity.getName())
        self.displayText.SetValue(self.charity.getIdentity().getDate())
        self.displayText.SetValue(json.dumps(self.charity, default=lambda o: o.__dict__, indent=4))
        
    def listCharities(self,e):
        string = self.client.listCharities()
        self.displayText.SetValue(string)
        
    def getCharityByName(self,e):
        """ Open a file"""
        dlg = wx.TextEntryDialog(self, "Charity Name", "Get Charity Details")
#        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(dlg.GetValue())
        
    def donateCharity(self,e):
        """ Open a file"""
        dlg = DonateCharityDialog(self)

        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(self.charity.getName())
        self.displayText.SetValue(self.charity.getIdentity().getDate())
        self.displayText.SetValue(json.dumps(self.charity, default=lambda o: o.__dict__, indent=4))
    
    def listTopics(self,e):
        string = self.client.listDiscussion()
        self.displayText.SetValue(string)
    
    def createTopic(self,e):
        dlg = CreateTopicDialog(self)
        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(self.topic.getTitle())
        self.displayText.SetValue(self.topic.getIdentity().getDate())
        self.displayText.SetValue(json.dumps(self.topic, default=lambda o: o.__dict__, indent=4))
        
    def getTopicByName(self,e):
        dlg = wx.TextEntryDialog(self, "Topic Name", "Get Topic Details")
        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(dlg.GetValue())
        
    def getTopicDetails(self,e):
        dlg = wx.TextEntryDialog(self, "Topic Name", "Get Topic Details")
        dlg.ShowModal()
        dlg.Destroy()
        self.displayText.SetValue(dlg.GetValue())
        
app = wx.App(False)
frame = MainWindow(None, "Reach Out Client")
app.MainLoop()
